import Util from 'core/js/util'
import Const from 'core/js/const'

function timeFormat(timestamp, format) {
	if (!timestamp) {
		return '-';
	}

    if (!format) {
        format = 'Y-m-d H:i:s';
    }
    return Util.date(format, timestamp);
}


function digitLength(data, length) {
    return Util.sprintf('%0' + length + 'd', data);
}

function currencyFormat(money, type) {
    money = parseInt(money);
    if (money % 100 === 0 && !type) {
        return Util.sprintf('%.2f', money / 100);
    }
    return Util.sprintf('%.2f', parseInt(money) / 100);
}

function imgUrl(img) {
    if (!img) {
        return img;
    }
    if (img.indexOf("http") >= 0) {
        return img;
    }
    return Const.NET.FILE_STATIC_URL + img;
}

function configLocation(type) {
	return Const.CONFIG_LOCATION[type];
}

function configType(type) {
	return Const.CONFIG_TYPE[type];
}

function trackTargetType(type) {
	return Const.TRACK_TYPE[type];
}

function strToJson(str) {
    return JSON.parse(str)
}





const _filters = {
    timeFormat: timeFormat,
    digitLength: digitLength,
    currencyFormat: currencyFormat,
    imgUrl: imgUrl,
	configLocation: configLocation,
	configType: configType,
	strToJson: strToJson,
	trackTargetType: trackTargetType
};

export default (Vue) => {
    Object.keys(_filters).forEach(key => {
        Vue.filter(key, _filters[key])
    })
}
